//
//  SYJEmoticonToolBar.swift
//  weibo
//
//  Created by 孙英建 on 16/4/25.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

// 设置button tag 使用
let tagNum = 1000

class SYJEmoticonToolBar: UIView {

    
    // 保存当前按钮
    var currentButton: UIButton?
    
    // 闭包传递按钮的点击
    var EmoticonToolBarButtonClosure: ((tag: Int) -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置空间
    func setupUI(){
        // 添加控件
        let defaultButton = addChildButton("compose_emotion_table_left", title: "默认", tag: 0)
        let emojiButton = addChildButton("compose_emotion_table_mid", title: "Emoji", tag: 1)
        let lxhButton = addChildButton("compose_emotion_table_right", title: "浪小花", tag: 2)
        
        // 添加约束
        defaultButton.snp_makeConstraints { (make) -> Void in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(emojiButton)
        }
        
        emojiButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(defaultButton.snp_trailing)
            make.width.equalTo(lxhButton)
        }
        
        lxhButton.snp_makeConstraints { (make) -> Void in
            make.top.trailing.bottom.equalTo(self)
            make.leading.equalTo(emojiButton.snp_trailing)
        }
        
        // 启动后将默认按钮设置为选中状态
        defaultButton.selected = true
//        defaultButton.enabled = false
        currentButton = defaultButton
        
    }
    
    // 添加底部按钮的方法
    func addChildButton(imgName: String, title: String, tag: Int) -> UIButton{
        let btn = UIButton()
        btn.tag = tag + tagNum
        
        btn.setBackgroundImage(UIImage(named: "\(imgName)_normal"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "\(imgName)_selected"), forState: UIControlState.Selected)
//        btn.setBackgroundImage(UIImage(named: "\(imgName)_selected"), forState: UIControlState.Disabled)
        
        btn.setTitle(title, forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        
        btn.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        addSubview(btn)
        
        return btn
    }
    
    
    // MARK: - 按钮的点击事件
    @objc private func buttonClick(sender: UIButton){
        
        if sender == currentButton{
            return
        }
        
        currentButton?.selected = false
//        currentButton?.enabled = true
        
        sender.selected = true
//        sender.enabled = false
        currentButton = sender
        
        // 调用闭包
        EmoticonToolBarButtonClosure!(tag: sender.tag - tagNum)
        
    }
    
    // 供外界调用修改按钮选中状态的方法
    func setEmoticonCurrentButton(section: Int){
        // 通过tag 获取指定的按钮
        let button = viewWithTag(section + tagNum) as! UIButton
        
        
        if button == currentButton{
            return
        }
        button.selected = true
        // 取消上次点击按钮的状态
        currentButton?.selected = false
        // 保存这次点击的按钮
        currentButton = button
    }

    

}

















